const MEvent={
	keyCallBacks:[],
	keyValue:{},
	eventContent:{},
	tickerValue:{},
}
MEvent.keyCount = function(data) {
	var count = 0
	for(let key in data) count++;
	return count;
}
//----键盘事件------
/**增加键盘事件帧听*/
MEvent.addKeyboard=(keyDownCb,keyUpCb,isDouble=true,doubleTime=300)=>{
	MEvent.removeKeyboard();
	window.addEventListener('keydown', keyDownCallBack,true);
	window.addEventListener('keyup', keyUpCallBack,true);
	function keyDownCallBack(e){
		let key=MEvent.keyToLower(e.key);
		//console.log(MEvent.keyValue[key]);
		if(!MEvent.keyValue.hasOwnProperty(key))	MEvent.keyValue[key]=1;//单击键盘
		else if(MEvent.keyValue[key]===0) 			MEvent.keyValue[key]=2;//双击键盘
		keyDownCb&&keyDownCb(e);
	}
	function keyUpCallBack(e){
		let key=MEvent.keyToLower(e.key);
		if(!isDouble){//不支持双击
			MEvent.keyValue[key]=2;
		}
		if(MEvent.keyValue[key]===1){
			MEvent.keyValue[key]=0;
			function clearKey(){
				if(MEvent.keyValue[key]===0){
					MEvent.removeValueByKey(key);
					keyUpCb&&keyUpCb(e);
				}
			}
			setTimeout(clearKey,doubleTime);
		}else if(MEvent.keyValue[key]===2){
			MEvent.removeValueByKey(key);
			keyUpCb&&keyUpCb(e);
		}
		
	}
	MEvent.keyCallBacks=[keyDownCallBack,keyUpCallBack];
	//console.log(MEvent.keyCallBacks);
}
/**删除键盘事件帧听*/
MEvent.removeKeyboard=()=>{
	if(MEvent.keyCallBacks.length===2){
		window.removeEventListener('keydown', MEvent.keyCallBacks[0],true);
		window.removeEventListener('keyup', MEvent.keyCallBacks[1],true);
		MEvent.keyCallBacks.length=0;
	}
}
MEvent.removeValueByKey=(key)=>{
	delete MEvent.keyValue[key];
}
//字母转小写
MEvent.keyToLower=(s)=>{
	return s.toLocaleLowerCase();
}
//字母转大写
MEvent.keyToUpper=(s)=>{
	return s.toLocaleUpperCase();
}
//----时间线事件------
MEvent.addTicket=(callBack,time=16.6,key='ticket')=>{
	MEvent.removeTicket(key);
	MEvent.tickerValue[key]=setInterval(() => {
		callBack&&callBack();
	}, time);
}
MEvent.removeTicket=(key='ticket')=>{
	if(MEvent.tickerValue[key]){
		clearInterval(MEvent.tickerValue[key]);
		delete MEvent.tickerValue[key];
	}
}
/**鼠标点击事件和双击事件*/
MEvent.click=(display,callBack,clickCnt=1)=>{
    display.addEventListener(clickCnt==1?'click':'dblclick',(e)=>{
        callBack(e);
    })
}
//----触控事件------
MEvent.touchStartEnd=(target,cbStart,cbEnd)=>{
	document.removeEventListener('touchstart',onTouchStart, false);
	document.removeEventListener('touchend',onTouchStart, false);
	document.addEventListener('touchstart',onTouchStart, false);
	document.addEventListener('touchend',onTouchEnd, false);
	let tp={x:0,y:0}
	function onTouchStart(e){
		let ts=e.touches;
		if(ts.length==0||ts.length>=2){//不支持没有和多个手指触控操作
			return;
		}
		[tp.x,tp.y]=[ts[0].clientX,ts[0].clientY];
		if(checkIsHit()){
			cbStart&&cbStart(target);
		}
	}
	function onTouchEnd(e){
		if(checkIsHit()){
			cbEnd&&cbEnd(target);
		}
	}
	function checkIsHit(){
		if(target.x<tp.x&&target.y<tp.y&&target.x+target.width>tp.x&&target.y+target.height>tp.y){
			return true;
		}
		return false;
	}
}
//----帧听发布事件------
//帧听事件
MEvent.on=(eventName,callBack)=>{
	if(!MEvent.eventContent[eventName]){
		MEvent.eventContent[eventName]=[]
	}
	MEvent.eventContent[eventName].push(callBack);
}
//发布事件
MEvent.emit=(eventName,data)=>{
	var a=MEvent.eventContent[eventName];
	if(a){
		for(var i=0;i<a.length;i++){
			if(data) a[i](data);
			else 	 a[i]();
		}
	}
}
//删除事件
MEvent.off=(eventName,callBack)=>{
	var a=MEvent.eventContent[eventName];
	if(a){
		var i=a.indexOf(callBack);
		if(i>=0){
			a.splice(i,1);
		}
	}
}
/**删除所有相同名字的事件*/
MEvent.offSameEventName=(eventName)=>{
	MEvent.eventContent[eventName]=null;
}